<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="专业名称" prop="majorName">
        <el-input
          v-model="queryParams.majorName"
          placeholder="专业名称"
          clearable
          @keyup.enter.native="searchButton"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="searchButton"
          >搜索</el-button
        >
        <el-button type="primary" size="mini" @click="handleQuery"
          >批量删除</el-button
        >
        <el-button type="primary" size="mini" @click="handleAdd"
          >添加专业</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="postList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="专业ID" align="center" prop="id" />
      <el-table-column label="专业名称" align="center" prop="majorName" />
      <el-table-column
        label="专业名称缩写"
        align="center"
        prop="majorAbbreviation"
      />
      <el-table-column
        label="专业描述"
        align="center"
        prop="majorDescription"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="ID" prop="id">
          <el-input v-model="form.id" placeholder="请输入ID" />
        </el-form-item> -->
        <el-form-item label="专业名称" prop="majorName">
          <el-input v-model="form.majorName" placeholder="请输入专业名称" />
        </el-form-item>
        <el-form-item label="专业名称缩写" prop="majorAbbreviation">
          <el-input
            v-model="form.majorAbbreviation"
            placeholder="请输入专业名称缩写"
          />
        </el-form-item>
        <el-form-item label="专业描述" prop="majorDescription">
          <el-input
            v-model="form.majorDescription"
            type="textarea"
            placeholder="请输入专业描述"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  searching,
  updatePost,
  addPost,
  delPost,
  listPost
} from '@/api/profession/post'

export default {
  name: 'Post',
  data () {
    return {
      loading: false,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      display: [],
      title: '',
      open: false,
      queryParams: {
        majorName: '',
        pageNum: 1,
        pageSize: 10
      },
      form: {
        id: '',
        majorName: '',
        majorAbbreviation: '',
        majorDescription: ''
      },
      rules: {
        majorName: [
          { required: true, message: '专业名称不能为空', trigger: 'blur' }
        ],
        majorAbbreviation: [
          { required: true, message: '专业名称缩写不能为空', trigger: 'blur' }
        ],
        majorDescription: [
          { required: true, message: '专业描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.searchingMajor()
    this.getList()
  },
  methods: {
    /** 查询岗位列表 */
    getList () {
      this.loading = true
      listPost(this.queryParams).then(response => {
        this.postList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    searchingMajor () {
      this.loading = true
      searching(this.queryParams)
        .then(response => {
          this.display = response
          this.total = response
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    },
    handleUpdate (row) {
      this.reset()
      searching(row).then(response => {
        this.form = response
        this.open = true
        this.title = '修改专业'
      })
    },
    handleAdd () {
      this.reset()
      this.open = true

      this.title = '添加专业'
    },
    submitForm () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            var json = {
              id: this.form.id,
              majorName: this.form.majorName,
              majorAbbreviation: this.form.majorAbbreviation,
              majorDescription: this.form.majorDescription
            }
            updatePost(json)
              .then(() => {
                this.$message.success('修改成功')
                this.open = false
                this.getList()
              })
              .catch(error => {
                console.error('Update error:', error)
              })
          } else {
            var json1 = {
              majorName: this.form.majorName,
              majorAbbreviation: this.form.majorAbbreviation,
              majorDescription: this.form.majorDescription
            }
            addPost(json1)
              .then(() => {
                this.$message.success('添加成功')
                this.open = false
                this.getList()
              })
              .catch(error => {
                console.error('Add error:', error)
              })
          }
        }
      })
    },

    searchButton () {
      this.searchingMajor()
    },
    cancel () {
      this.open = false
      this.reset()
    },
    reset () {
      this.form = {
        majorName: '',
        majorAbbreviation: '',
        majorDescription: ''
      }
      if (this.$refs['form']) {
        this.$refs['form'].resetFields()
      }
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const postIds = row.id
      this.$modal
        .confirm('是否确认删除岗位编号为"' + postIds + '"的数据项？')
        .then(function () {
          return delPost(postIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    }
  }
}
</script>
